package luo.handler;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author:let_free
 * @Date:2024/9/17/21:20
 **/
@Component
public class SimpleWebSocketHandler extends TextWebSocketHandler {
    //    管理会话列表
    private List<WebSocketSession> sessions = new ArrayList<>();

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        sessions.add(session);
    }
    public void sendMessage(String message) throws IOException {
        for(WebSocketSession session:sessions){
            session.sendMessage(new TextMessage(message));
        }
    }
}
